package com.situ.company.employee.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.EmployeeService;
import com.situ.company.util.AuthCodeServlet;
import com.situ.company.util.FmtRequest;
import com.situ.company.util.MD5;
import com.situ.company.util.UploadUtil;

@WebServlet("/employee") // 接收请求
public class EmployeeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// req.setCharacterEncoding("UTF-8");
		// resp.setCharacterEncoding("UTF-8");
		String action = req.getParameter("action");
		String result = "";
		switch (action) {
		case "reg":
			result = reg(req);
			break;
		case "login":
			result = login(req);
			break;
		case "add":
			result = add(req);
			break;
		case "sel":
			result = sel(req);
			break;
		case "del":
			result = del(req);
			break;
		case "get":
			result = get(req);
			break;
		case "upd":
			result = upd(req);
			break;
		case "list":
			result = list(req);
			break;
		case "resetPass":
			result = resetPass(req);
			break;
		case "updpass":
			result = updpass(req);
			break;
		case "setdept":
			result = setdept(req);
			break;
		case "delpic":
			result = delpic(req);
			break;
		case "logout":
			req.getSession().removeAttribute("user");
			resp.sendRedirect(req.getContextPath() + "/web/login.jsp");
			return;
		default:
			break;
		}
		FmtRequest.out(resp, result);

	}

	/**
	 * 删除图片
	 * 
	 * @param req
	 * @return
	 */
	private String delpic(HttpServletRequest req) {
		String code = req.getParameter("code");
		EmployeeModel model = new EmployeeModel(code);
		EmployeeModel dbm = service.selectModel(model);
		String image = dbm.getImage();
		UploadUtil.delFile(image);
		model.setImage("");
		return service.update(model);
	}

	/**
	 * 设置部门
	 * 
	 * @param req
	 * @return
	 */
	private String setdept(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String codeDept = req.getParameter("codeDept");
		EmployeeModel model = new EmployeeModel(code);
		model.setCodeDept(codeDept);
		return service.update(model);
	}

	/**
	 * 修改密码
	 * 
	 * @param req
	 * @return
	 */
	private String updpass(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String pass = req.getParameter("pass");
		pass = MD5.encode(pass);
		EmployeeModel model = new EmployeeModel(code, pass);
		return service.update(model);
	}

	/**
	 * 重置密码
	 * 
	 * @param req
	 * @return
	 */
	private String resetPass(HttpServletRequest req) {
		String code = req.getParameter("code");
		EmployeeModel model = new EmployeeModel(code);
		model.setPass(getDefaultPass());
		return service.update(model);
	}

	/**
	 * 分页
	 * 
	 * @param req
	 * @return
	 */
	private String list(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String name = req.getParameter("name");
		String pageIndex = req.getParameter("pageIndex");// 获取参数
		String pageLimit = req.getParameter("pageLimit");
		EmployeeModel model = new EmployeeModel(code, name, null);
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		Map<String, Object> map = new HashMap<>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return new JSONObject(map).toString();
	}

	/**
	 * 修改
	 * 
	 * @param req
	 * @return
	 */
	private String upd(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String name = req.getParameter("name");
		String codeDept = req.getParameter("codeDept");
		EmployeeModel model = new EmployeeModel(code, name, codeDept);
		return service.update(model);
	}

	/**
	 * 修改时回显
	 * 
	 * @param req
	 * @return
	 */
	private String get(HttpServletRequest req) {
		String code = req.getParameter("code");
		EmployeeModel model = new EmployeeModel(code);
		EmployeeModel mdb = service.selectModel(model);
		return new JSONObject(mdb).toString();
	}

	/**
	 * 删除
	 * 
	 * @param req
	 * @return
	 */
	private String del(HttpServletRequest req) {
		String code = req.getParameter("code");
		EmployeeModel model = new EmployeeModel(code);
		return service.delete(model);
	}

	/**
	 * 查询
	 * 
	 * @param req
	 * @return
	 */
	private String sel(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String name = req.getParameter("name");
		EmployeeModel model = new EmployeeModel(code, name, null);
		List<EmployeeModel> list = service.selectList(model);
		return new JSONArray(list).toString();
	}

	// private void out(HttpServletResponse resp, String result) throws
	// IOException {
	// PrintWriter out = resp.getWriter();
	// out.append(result);// 返回结果
	// out.flush();
	// out.close();
	// out = null;
	// }

	/**
	 * 添加员工信息
	 * 
	 * @param req
	 * @return
	 */
	private String add(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String name = req.getParameter("name");
		String pass = req.getParameter("pass");
		String codeDept = req.getParameter("codeDept");
		if (pass == null || pass.trim().isEmpty())
			pass = getDefaultPass();// 默认密码
		else
			pass = MD5.encode(pass);
		EmployeeModel model = new EmployeeModel(code, name, pass, codeDept);

		return service.insert(model);
	}

	private String getDefaultPass() {
		return MD5.encode("1234");// 默认密码
	}

	private EmployeeService service = new EmployeeService();

	/**
	 * 登录
	 * 
	 * @param req
	 * @return
	 */
	private String login(HttpServletRequest req) {// 加密
		// EmployeeModel model = new EmployeeModel(code, pass);// 封装对象
		String authCode = req.getParameter("authCode");
		Object authCodeSession = req.getSession().getAttribute(AuthCodeServlet.name);
		if (!authCodeSession.toString().equals(authCode))// 校验
			return "L";
		// return service.login(model);
		// 账号登录 返回0账号不存在，1登录成功，2密码错误

		String code = req.getParameter("code");// 获取参数
		EmployeeModel model = new EmployeeModel(code);
		EmployeeModel mdb = service.selectModel(model);
		if (mdb == null)
			return "0";
		String pass = req.getParameter("pass");
		pass = MD5.encode(pass);
		// if (!mdb.getPass().equals(model.getPass()))
		if (!mdb.getPass().equals(pass))
			return "2";
		req.getSession().setAttribute("user", mdb);
		return "1";

		// return mdb.getPass().equals(model.getPass()) ? "1":"2";
	}

	/**
	 * 注册
	 * 
	 * @param req
	 * @return
	 */
	private String reg(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String name = req.getParameter("name");
		String pass = req.getParameter("pass");
		pass = MD5.encode(pass);
		// System.out.println(code + "\t" + name + "\t" + pass);
		String authCode = req.getParameter("authCode");

		Object authCodeSession = req.getSession().getAttribute(AuthCodeServlet.name);
		if (!authCodeSession.toString().equals(authCode)) {// 校验
			return "L";
		}

		EmployeeModel model = new EmployeeModel(code, name, pass, null);// 封装对象

		return service.insert(model);// 调用方法

	}

}
